<?php
namespace application\datatypes;
use application\datatypes\DataType;
use application\utilities\Utility;

class GeshiCode extends DataType {
	public static $name = 'GeSHi Syntax Highlighter';
	public static $fields = array(array('columnType'=>'LONGTEXT'));
	
	public static function displayJS($field) {
		Modulator()->includeScript(CN_URL.'modules/geshi-syntax-highlighter/scripts/shCore.js');
		Modulator()->includeScript(CN_URL.'modules/geshi-syntax-highlighter/scripts/shBrushPhp.js');
		?><link type="text/css" rel="stylesheet" href="<?=CN_URL?>modules/geshi-syntax-highlighter/styles/shCoreDefault.css"/><?php
		/*?><link rel="stylesheet" type="text/css" media="all" href="<?= CN_URL ?>modules/geshi-syntax-highlighter/geshi/css/default.css" /><?php*/?>
		<script type="text/javascript">
		$(document).ready(function() {
			SyntaxHighlighter.all();
		});
		</script>
		<?php
	}
	public static function formJS($field) {
		Modulator()->includeScript('jquery');
		Modulator()->includeScript(getCommonFile('third-party/jquery-linedtextarea/jquery-linedtextarea.js'));
		Modulator()->includeScript(CN_URL.'modules/geshi-syntax-highlighter/insert.js');
		
		
		?><link rel="stylesheet" type="text/css" media="all" href="<?= getCommonFile('third-party/jquery-linedtextarea/jquery-linedtextarea.css') ?>" />
		
		
		<script type="text/javascript">
			$(document).ready(function() {
				$('#<?=$field->cf_column?>').linedtextarea();
				$('#<?=$field->cf_column?>').keydown(function(event) {
					if(event.keyCode == '9') {
						event.preventDefault();
						insertAtCaret('<?=$field->cf_column?>', "\t");
						
					}
					
				});
			});
		</script>
		<?php
		
	}
	
	public static function validate($field, $data, $results) {
		if($field->cf_required && !$data[$field->cf_column]) {
			$results['errors'][] = '<b>'.$field->cf_display_name.'</b> is required.';
		}
		elseif($value = $data[$field->cf_column]) {
			$results['db_fields'][$field->cf_column] = htmlentities($value);	
		}
		
		return $results;
	}
	
	public static function display($formElements) {
		?>
		<fieldset class="single-field">
			<legend><?=$formElements['display']?></legend>
			<?php if($description = $formElements['description']) : ?>
				<p class="form-description"><?=$description?></p>
			<?php endif;?>
			<?=$formElements['elements']['element']?>
		</fieldset>
		<?php
	}
	
	public static function build($field, $data) {
		$value = html_entity_decode($data[$field->cf_column]);
		$formElements = array('description'=>$field->cf_description, 'column'=>$field->cf_column, 'display'=>$field->cf_display_name, 'data_type'=>$field->cf_data_type, 'elements'=>array(
						'element'=>
							sprintf('<textarea class="code" name="%1$s" id="%1$s">%2$s</textarea>', $field->cf_column, $value)
						
		));
		return $formElements;
	
	}
	
	public static function prepareData($field, $data, $params) {
		include_once(CN_DOCUMENT_ROOT.'modules/geshi-syntax-highlighter/geshi/geshi.php');
		
		/**$language = $params['language'];
		
		$code = html_entity_decode($data[$field->cf_column]);
		
		$geshi = new \GeSHi($code, $language);
		
		$geshi->enable_classes();
		$geshi->set_overall_id($field->cf_column . '_id');
		$geshi->set_overall_class('geshi-code-inner');
		$parts = array('symbols', 'escape_characters', 'strings', 'numbers', 'methods', 'comments', 'keywords');
		
		switch($params['lineNumbers']) {
			case 'normal':
				$flag = GESHI_NORMAL_LINE_NUMBERS;
				break;
			case 'fancy':
				$flag = GESHI_FANCY_LINE_NUMBERS;
				break;
			default:
				$flag = GESHI_NO_LINE_NUMBERS;
				break;
			
		}
		$geshi->enable_line_numbers($flag);
		
		foreach($parts as $part) {
			if(isset($params[$part])) {
				$function = 'set_'.$part.'_highlighting';
				$geshi->$function($params[$part]['highlighting']);
			}
		}
		$code = $geshi->parse_code();
		
		if($function = $params['callback']) {
			$code = $function($code);
		}*/
		
		
		
		return '<div class="geshi-code"><pre class="brush: php;">'.$data[$field->cf_column].'</pre></div>';
	}
	
	public static function editForm($field) {
		?>
		<div class="box-content">
			<form method="post">
			<input type="hidden" name="field_id" value="<?=$field->getId()?>"/>
			<p>
			<label>Field Display Name</label>
			<input type="text" name="field_display_name" class="big wide" value="<?=$field->getDisplayName()?>"/>
			</p>
			<p>
			<label>Field Description</label>
			<textarea name="field_description" class="wide"><?=$field->getDescription()?></textarea>
			</p>
			<p>
				<label><input type="checkbox" name="field_required" <?=($field->getRequired()) ? 'checked="checked"' : ''?> value="1"/> Required</label>
				<label><input type="checkbox" name="field_show" value="1" <?=($field->getShow()) ? 'checked="checked"' : ''?>/> Show in Manage Panel</label>
				
			</p>
			
			
			<p><input type="submit" name="update_field" class="small-button" value="Submit Changes"/></p>
			</form>
		</div>
			<?php
	}
	
}
?>